package com.letoken.platform.app.job;

import com.letoken.platform.repo.app.dao.AdminFundLevelConfigMapper;
import com.letoken.platform.repo.app.dao.AppFundLevelInfoMapper;
import com.letoken.platform.repo.app.po.AdminFundLevelConfig;
import com.letoken.platform.repo.app.po.AppFundDetail;
import com.letoken.platform.repo.app.po.AppFundInfo;
import com.letoken.platform.repo.app.po.AppFundLevelInfo;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Component
public class UpdateFundLevelJob {

    @Resource
    AppFundLevelInfoMapper appFundLevelInfoMapper;

    @Resource
    private AdminFundLevelConfigMapper adminFundLevelConfigMapper;

    @XxlJob("updateFundLevelJob")
    @Transactional
    public  void updateFundLevelJob(){
       List<Integer> list = appFundLevelInfoMapper.getUserList();
        for (Integer userId:list) {
            updateLevel(userId);
        }
    }


    @XxlJob("addFundCard")
    @Transactional
    public  void addFundCard(){
        List<AppFundInfo> list = appFundLevelInfoMapper.addFundWeek();
        for (AppFundInfo appFundInfo:list) {
            AppFundInfo fundInfo = new AppFundInfo();
            fundInfo.setUserId(appFundInfo.getUserId());
            fundInfo.setFundId(1);
            fundInfo.setRate(new BigDecimal("0.00233300"));
            fundInfo.setAmount(appFundInfo.getFund().multiply(new BigDecimal("0.00233300")));
            fundInfo.setFund(appFundInfo.getFund());
            fundInfo.setExpirationTime(LocalDateTime.now().plusDays(7));
            fundInfo.setCreateTime(LocalDateTime.now());
            fundInfo.setType(1);
            fundInfo.setState(2);

            AppFundDetail appFundDetail1 = new AppFundDetail();
            appFundDetail1.setUserId(appFundInfo.getUserId());
            appFundDetail1.setType(6);
            appFundDetail1.setAmount(appFundInfo.getFund());
            appFundDetail1.setCreateTime(LocalDateTime.now());
            appFundDetail1.setState(appFundInfo.getFundId());

            if(appFundInfo.getFund().compareTo(new BigDecimal("2000"))>=1){
                for (int i = 0; i < 2; i++) {
                    fundInfo.insert();
                    appFundDetail1.insert();
                }
            }else{
                fundInfo.insert();
                appFundDetail1.insert();
            }
        }
    }


    private void updateLevel(Integer inviterId ){
        AppFundLevelInfo info = appFundLevelInfoMapper.selectById(inviterId);
        List<AdminFundLevelConfig> levelConfigs = adminFundLevelConfigMapper.getLevelList();
        if(info==null || info.getLevel()<levelConfigs.size()){
            Integer count = appFundLevelInfoMapper.getFundByInviterIdCOunt(inviterId);
            Integer count2 = appFundLevelInfoMapper.getFundByUserId(inviterId);
            if(count2>0){
                for (AdminFundLevelConfig adminFundLevelConfig:levelConfigs) {
                    if (count >= adminFundLevelConfig.getCondition()){
                        AppFundLevelInfo appFundLevelInfo = new AppFundLevelInfo();
                        appFundLevelInfo.setLevel(adminFundLevelConfig.getLevel());
                        appFundLevelInfo.setUserId(inviterId);
                        appFundLevelInfo.setUpdateTime(LocalDateTime.now());
                        appFundLevelInfo.insertOrUpdate();
                        break;
                    }
                }
            }else {
                AppFundLevelInfo appFundLevelInfo = new AppFundLevelInfo();
                appFundLevelInfo.setLevel(0);
                appFundLevelInfo.setUserId(inviterId);
                appFundLevelInfo.insertOrUpdate();
            }
        }
    }
}
